package com.xinou.daily_report.expand.sso.mini;

import com.xinou.daily_report.config.DailyReportYml;
import com.xinou.sso.config.shiro.jwt.JwtCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

/**
 *
 * @author lizhongyuan
 * @date 2021/6/21
 */
@Configuration
public class MiniConfig {

    /**
     * mini Realm
     */
    @Bean
    public MiniRealm miniRealm(EhCacheManager cacheManager, DailyReportYml dailyReportYml) {
        MiniRealm miniRealm = new MiniRealm();
        miniRealm.setCredentialsMatcher(new MiniCredentialsMatcher());
        miniRealm.setAuthenticationCacheName(dailyReportYml.getDailyReportAuthenticationCacheName());
        miniRealm.setAuthorizationCacheName(dailyReportYml.getDailyReportAuthorizationCacheName());
        miniRealm.setAuthorizationCachingEnabled(true);
        miniRealm.setAuthenticationCachingEnabled(true);
        miniRealm.setCacheManager(cacheManager);
        return miniRealm;
    }

    @Bean
    public MiniFilter miniFilter() {
        return new MiniFilter();
    }

    @Bean
    public FilterRegistrationBean<MiniFilter> miniFilterRegistrationBean(MiniFilter miniFilter) {
        FilterRegistrationBean<MiniFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(miniFilter);
        registration.setEnabled(false);
        registration.setName("miniFilterRegistrationBean");
        return registration;
    }

}